﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Business.Mst;
using ESOL.Business.Pur;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;

namespace ESOL.WinUI.Pur
{
    public partial class frmMaintainPurchaseArrivedQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable

        private bool mMultiSelected = false;
        private List<PurPurchaseArrivedEntity> mSelectedPurchaseOrders;
        public List<PurPurchaseArrivedEntity> SelectedPurchaseOrders
        {
            get { return mSelectedPurchaseOrders; }
        }
        private PurPurchaseArrivedEntity mSelectedPurchaseArrived;
        public PurPurchaseArrivedEntity SelectedPurchaseArrived
        {
            get { return mSelectedPurchaseArrived; }
        }
        #endregion

        #region Constructor
        public frmMaintainPurchaseArrivedQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND E.SUPPLIER_ID = '" + supplier.SupplierId + "' ";
            }
            if (string.IsNullOrEmpty(txtOrderCode.Text) == false)
            {
                condition += " AND A.ARRIVED_CODE = '" + txtOrderCode.Text + "' ";
            }
            if (dtpOrderDateFrom.EditValue!=null)
            {
                condition += " AND A.ARRIVED_DATE >= CONVERT(DATETIME,'" + dtpOrderDateFrom.EditValue + "')";
            }
            if (dtpOrderDateTo.EditValue!=null)
            {
                condition += " AND A.ARRIVED_DATE <= CONVERT(DATETIME,'" + dtpOrderDateTo.EditValue + "')";
            }
            if (cbIsQualityChecked.Checked)
            {
                condition += " AND A.IS_QUALITYCHECKED = 1 ";
            }
            else
            {
                condition += " AND A.IS_QUALITYCHECKED = 0 ";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "A.ARRIVED_CODE DESC";
            DataTable dt = PurPurchaseArrivedBs.GetByConditionForQuery(condition, "", orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        protected override void OnShown(EventArgs e)
        {
            dtpOrderDateFrom.EditValue = DateTime.Now.AddDays(-3);
            BindDataGrid();
            //if (mMultiSelected == false)
            //{
            //    gvMain.Columns["colSelected"].Visible = false;
            //}
            base.OnShown(e);
        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_Confirm()
        {
            if (mSelectedPurchaseArrived == null)
            {
                MessageBox.Show("请选择一个采购到货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPurchaseArrived = new PurPurchaseArrivedEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedPurchaseArrived == null)
            {
                MessageBox.Show("请选择一个采购到货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.OK;
        }


    }
}